/*******************************************************************************
* File Name: CSD_Touchpad_GESTURE.h
* Version 2.40
*
* Description:
*  This file provides constants and parameter values for the Trackpad with
*  Gestures APIs for the CapSense CSD component.
*
* Note:
*
********************************************************************************
* Copyright 2014-2015, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#if !defined(CY_CAPSENSE_CSD_GESTURE_CSD_Touchpad_H)
#define CY_CAPSENSE_CSD_GESTURE_CSD_Touchpad_H

#include "CSD_Touchpad_CSHL.h"
#include "CSD_Touchpad_TMG.h"
#include "CSD_Touchpad_PVT.h"


/***************************************
*   Condition compilation parameters
***************************************/

#define CSD_Touchpad_CLICK_GEST_ENABLED             (1u)
#define CSD_Touchpad_ZOOM_GEST_ENABLED              (1u)
#define CSD_Touchpad_ST_SCROLL_GEST_ENABLED         (0u)
#define CSD_Touchpad_DT_SCROLL_GEST_ENABLED         (0u)
#define CSD_Touchpad_FLICK_GEST_ENABLED             (0u)
#define CSD_Touchpad_ROTATE_GEST_ENABLED            (0u)
#define CSD_Touchpad_EDGE_SWIPE_GEST_ENABLED        (1u)

/* Condition to enable Trackpad with Gestures API  */
#define CSD_Touchpad_TRACKPADS_WITH_GESTURES       (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT)

/* IIR filter coefficient */
#define CSD_Touchpad_XY_IIR_COEF                    (0x08u)

/* Mask for RAW and POS filters */
#define CSD_Touchpad_XY_IIR_MASK                    (0x08u)
#define CSD_Touchpad_XY_JITTER_MASK                 (0x10u)
#define CSD_Touchpad_XY_BALLISTIC_MASK              (0x40u)
#define CSD_Touchpad_XY_ADAPTIVE_IIR_MASK           (0x80u)

/* Ballistic Multiplier */
#if (0u != (CSD_Touchpad_XY_BALLISTIC_MASK & CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK))
#define CSD_Touchpad_BALLISTIC_EN                   (1u)
#else
#define CSD_Touchpad_BALLISTIC_EN                   (0u)
#endif /* (0u != (CSD_Touchpad_XY_BALLISTIC_MASK & CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK)) */

/* XY IIR filter coefficients */
#define CSD_Touchpad_XY_IIR_DISABLED                (0u)
#define CSD_Touchpad_XY_IIR_2                       (1u)
#define CSD_Touchpad_XY_IIR_4                       (2u)
#define CSD_Touchpad_XY_IIR_8                       (3u)
#define CSD_Touchpad_XY_IIR_16                      (4u)
#define CSD_Touchpad_XY_IIR_COEF_MASK               (7u)

/* XY filters initialize flag */
#define CSD_Touchpad_XY_FILTERS_INITIALIZED         (3u)
#define CSD_Touchpad_XY_FILTERS_NOT_INITIALIZED     (0u)

#define CSD_Touchpad_X0Y0_FILTERS_INITIALIZED       (1u)
#define CSD_Touchpad_X0Y0_FILTERS_NOT_INITIALIZED   (0u)

#define CSD_Touchpad_X1Y1_FILTERS_INITIALIZED       (2u)
#define CSD_Touchpad_X1Y1_FILTERS_NOT_INITIALIZED   (0u)

/* Enable XY filtering for the second finger*/
#define CSD_Touchpad_XY_SECOND_FINGER_FILTER_EN     (0u)


/***************************************
*           API Constants
***************************************/

/* Widgets constants definition */
#define CSD_Touchpad_TRACKPAD__TPG                  (0u)

/* Mask for Trackpad with Gestures position filters */
#define CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK    (CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK)

/* Constant to check if any position filter exists  */
#define CSD_Touchpad_TP_GESTURE_ANY_POS_FILTER      ( CSD_Touchpad_XY_IIR_MASK | \
                                                           CSD_Touchpad_XY_JITTER_MASK | \
                                                           CSD_Touchpad_XY_ADAPTIVE_IIR_MASK )

/* Filters Number */
#define CSD_Touchpad_TP_FILTERS_NUMBER              (3u)

/* Offsets for X and Y coordinates */
#define CSD_Touchpad_TP_GESTURE_POS_IIR             (0u)
#define CSD_Touchpad_TP_GESTURE_POS_AF              (4u)
#define CSD_Touchpad_TP_GESTURE_POS_JIT             (8u)

/* Fingers identifiers */
#define CSD_Touchpad_FIRST_FINGER_ID                (0u)
#define CSD_Touchpad_SECOND_FINGER_ID               (1u)

/* TMG_BallisticsMultiplier offsets */
#define CSD_Touchpad_FIRST_FINGER_OFFSET            (0u)
#define CSD_Touchpad_SECOND_FINGER_OFFSET           (2u)
#define CSD_Touchpad_DX_ID                          (0u)
#define CSD_Touchpad_DY_ID                          (1u)

/* Maximums for X and Y coordinates */
#define CSD_Touchpad_TP_GESTURE_POS_X_MAX           (4000u)
#define CSD_Touchpad_TP_GESTURE_POS_Y_MAX           (4000u)

/* Centroid Errors */
#define CSD_Touchpad_POSITION_ERROR                 (0xFFFFFFFFu)
#define CSD_Touchpad_FINGER_ERROR                   (0xFFu)

/* Previous Position Initial */
#define CSD_Touchpad_POSITION_NO_FINGER             (-1)

/***************************************
*        Defines for Gesture Codes
***************************************/
/* Code for no gesture detected case */
#define CSD_Touchpad_NO_GESTURE                  (0x00u)

/* Single touch click gestures */
#define CSD_Touchpad_ST_CLICK                    (0x20u)
#define CSD_Touchpad_ST_DOUBLECLICK              (0x22u)
#define CSD_Touchpad_CLICK_AND_DRAG              (0x24u)

/* Rotate gestures */
#define CSD_Touchpad_ROTATE_CW                   (0x28u)
#define CSD_Touchpad_ROTATE_CCW                  (0x29u)

/* TouchDown and Lift Off Gestures */
#define CSD_Touchpad_TOUCHDOWN                   (0x2Fu)
#define CSD_Touchpad_LIFT_OFF                    (0x4Fu)

/* ST Scroll gestures */
#define CSD_Touchpad_ST_SCROLL_NORTH             (0xC0u)
#define CSD_Touchpad_ST_SCROLL_SOUTH             (0xC2u)
#define CSD_Touchpad_ST_SCROLL_WEST              (0xC4u)
#define CSD_Touchpad_ST_SCROLL_EAST              (0xC6u)
#define CSD_Touchpad_ST_INERTIAL_SCROLL_NORTH    (0xB0u)
#define CSD_Touchpad_ST_INERTIAL_SCROLL_SOUTH    (0xB2u)
#define CSD_Touchpad_ST_INERTIAL_SCROLL_WEST     (0xB4u)
#define CSD_Touchpad_ST_INERTIAL_SCROLL_EAST     (0xB6u)

/* DT Scroll gestures */
#define CSD_Touchpad_DT_SCROLL_NORTH             (0xC8u)
#define CSD_Touchpad_DT_SCROLL_SOUTH             (0xCAu)
#define CSD_Touchpad_DT_SCROLL_WEST              (0xCCu)
#define CSD_Touchpad_DT_SCROLL_EAST              (0xCEu)
#define CSD_Touchpad_DT_INERTIAL_SCROLL_NORTH    (0xB8u)
#define CSD_Touchpad_DT_INERTIAL_SCROLL_SOUTH    (0xBAu)
#define CSD_Touchpad_DT_INERTIAL_SCROLL_WEST     (0xBCu)
#define CSD_Touchpad_DT_INERTIAL_SCROLL_EAST     (0xBEu)

/* Dual touch click gesture */
#define CSD_Touchpad_DT_CLICK                    (0x40u)

/* Zoom Gestures */
#define CSD_Touchpad_ZOOM_IN                     (0x48u)
#define CSD_Touchpad_ZOOM_OUT                    (0x49u)

/* Flick gestures */
#define CSD_Touchpad_FLICK_NORTH                 (0x50u)
#define CSD_Touchpad_FLICK_NORTH_EAST            (0x52u)
#define CSD_Touchpad_FLICK_EAST                  (0x54u)
#define CSD_Touchpad_FLICK_SOUTH_EAST            (0x56u)
#define CSD_Touchpad_FLICK_SOUTH                 (0x58u)
#define CSD_Touchpad_FLICK_SOUTH_WEST            (0x5Au)
#define CSD_Touchpad_FLICK_WEST                  (0x5Cu)
#define CSD_Touchpad_FLICK_NORTH_WEST            (0x5Eu)

/* Edge swipe gestures */
#define CSD_Touchpad_EDGE_SWIPE_LEFT             (0xA0u)
#define CSD_Touchpad_EDGE_SWIPE_RIGHT            (0xA2u)
#define CSD_Touchpad_EDGE_SWIPE_TOP              (0xA4u)
#define CSD_Touchpad_EDGE_SWIPE_BOTTOM           (0xA6u)
#define CSD_Touchpad_EDGE_SWIPE_MASK             (0xA0u)   /*The Mask for the Edge Swipe Gestures */

/* Gesture groups masks */
#define CSD_Touchpad_GROUP1_MASK                 (0x10u)
#define CSD_Touchpad_GROUP2_MASK                 (0x20u)
#define CSD_Touchpad_GROUP3_MASK                 (0x40u)
#define CSD_Touchpad_GROUP4_MASK                 (0x80u)

/* Invalid coordinates */
#define CSD_Touchpad_INVALID_COORDINATE          (0xFFFFu)


/***************************************
*         Parameters
***************************************/
#define CSD_Touchpad_ADV_PENULTIMATE_THRESHOLD         (1u)
#define CSD_Touchpad_ADV_VIRTUAL_SENSOR_THRESHOLD      (20u)
#define CSD_Touchpad_ADV_CROSS_COUPLING_THRESHOLD      (1u)

#define CSD_Touchpad_PENULTIMATE_THRESHOLD          (CSD_Touchpad_ADV_PENULTIMATE_THRESHOLD + CSD_Touchpad_ADV_CROSS_COUPLING_THRESHOLD)
#define CSD_Touchpad_VIRTUAL_SENSOR_THRESHOLD       (CSD_Touchpad_ADV_VIRTUAL_SENSOR_THRESHOLD + CSD_Touchpad_ADV_CROSS_COUPLING_THRESHOLD)
#define CSD_Touchpad_CROSS_COUPLING_THRESHOLD       (CSD_Touchpad_ADV_CROSS_COUPLING_THRESHOLD)

/* Number of supported fingers */
#define CSD_Touchpad_MAX_FINGER                     (2u)

/* Ballistic Multiplier Parameters */
#define CSD_Touchpad_ACCELERATION_LEVEL            (9u)
#define CSD_Touchpad_SPEED_LEVEL                   (2u)
#define CSD_Touchpad_DIVISOR_VALUE                 (4u)
#define CSD_Touchpad_SPEED_THRESHOLD_X             (1u)
#define CSD_Touchpad_SPEED_THRESHOLD_Y             (1u)

/* Adaptive Filter Parameters */
#define CSD_Touchpad_MAX_FILTER_COEF            (60u)
#define CSD_Touchpad_MIN_FILTER_COEF            (4u)
#define CSD_Touchpad_LARGE_MOVE_THRESHOLD       (80u)
#define CSD_Touchpad_LITTLE_MOVE_THRESHOLD      (6u)
#define CSD_Touchpad_NO_MOVE_THRESHOLD          (2u)
#define CSD_Touchpad_DIVISOR                    (64u)


/***************************************
*        Structures
***************************************/

/* Touch position type */
typedef struct {
    uint16 x;
    uint16 y;
} CSD_Touchpad_POSITION_STRUCT;

/* Touch position type */
typedef struct {
    int16 x;
    int16 y;
} CSD_Touchpad_DIFF_POSITION_STRUCT;

/* Adaptive Filter type */
typedef struct
{
    uint8   maxK;     /* Max Filter Coefficient */
    uint8   minK;     /* Min Filter Coefficient */
    uint8   noMovTh;     /* No Movement Threshold */
    uint8   littleMovTh; /* Little Movement Threshold */
    uint8   largeMovTh;  /* Large Movement Threshold */
    uint8   divVal;      /* Divisor Value */
}ADAPTIVE_FILTER_OPS_P4_v2_40;

/* Advanced Centroid pointers type */
typedef struct
{
    uint16 (*ptrGet16bitDiffCountData)(uint32 sensor);  /* Pointer to CSD_Touchpad_sensorSignal[] */
    uint16 (*ptrGet16bitFingerThreshold)(uint32 widget);  /* Pointer to CSD_Touchpad_fingerThreshold[] */
    uint16 (*ptrGet16bitNoiseThreshold)(uint32 widget);   /* Pointer to CSD_Touchpad_noiseThreshold[] */
    uint16  *positionZ;             /* Pointer to the array - Z position */
    uint32  *centroidMult;          /* Pointer to CSD_Touchpad_centroidMult[] */
    uint8   *maxPos;                /* Pointer to CSD_Touchpad_maxPos[] */
    uint8 const *rawDataIndex;      /* Pointer to CSD_Touchpad_rawDataIndex[] */
}CSD_Touchpad_ADV_CENTROID_POINTERS_P4_v2_40;

/* Advanced Centroid variables type */
typedef struct
{
    uint16  penultimateThreshold;   /* CSD_Touchpad_PENULTIMATE_THRESHOLD const */
    uint16  virtualSensorThreshold; /* CSD_Touchpad_VIRTUAL_SENSOR_THRESHOLD const */
    uint16  crossCouplingThreshold; /* CSD_Touchpad_CROSS_COUPLING_THRESHOLD const */
    uint8   sliderXCount;           /* Number of segments on X axis - const */
    uint8   segCount;               /* Number of segments on both axis - const */
    uint8   maxFinger;              /* Number of supported fingers - const */
    uint8   widgetTPG;              /* Trackpad with Gestures widget */
    CSD_Touchpad_ADV_CENTROID_POINTERS_P4_v2_40 * pointers;
}CSD_Touchpad_ADV_CENTROID_P4_v2_40;


/***************************************
*        Function Prototypes
***************************************/

#if (0u != CSD_Touchpad_TRACKPADS_WITH_GESTURES)
    uint32 CSD_Touchpad_DecodeAllGestures(uint32 touchNumber, const CSD_Touchpad_POSITION_STRUCT *pos);
    #define CSD_Touchpad_GetScrollCnt() CSD_Touchpad_TMG_GetScrollCnt()
    /*  High level Functions Prototypes   */
    uint32 CSD_Touchpad_GetDoubleTouchCentroidPos(CSD_Touchpad_POSITION_STRUCT *pos);
    #if (0u != CSD_Touchpad_BALLISTIC_EN)
        uint32 CSD_Touchpad_GetDiffDoubleCentroidPos(CSD_Touchpad_DIFF_POSITION_STRUCT *pos);
    #endif /* (0u != CSD_Touchpad_BALLISTIC_EN) */

    /*  Internal Functions Prototypes     */
    void CSD_Touchpad_AdvancedCentroidInit(void);
    #if (0u != CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK)
        void CSD_Touchpad_ApplyPositionFilters(uint32 fingerID, CSD_Touchpad_POSITION_STRUCT pos[]);
    #endif /* (0u != CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK) */

    #if (0u != (CSD_Touchpad_XY_IIR_MASK & CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK))
        uint16 CSD_Touchpad_IIRXFilter(uint16 x1, uint16 x2, uint32 iirFilterCoef);
    #endif /* (0u != (CSD_Touchpad_XY_IIR_MASK & \
            *          CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK))
            */

    #if (0u != (CSD_Touchpad_XY_ADAPTIVE_IIR_MASK & CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK))
        void AdaptiveFilterInit_P4_v2_40(ADAPTIVE_FILTER_OPS_P4_v2_40 *options);
        void AdaptiveFilter_P4_v2_40(uint16 *curX, uint16 *curY, uint16 *prevX, uint16 *prevY);
    #endif /* (0u != (CSD_Touchpad_XY_ADAPTIVE_IIR_MASK & \
                        *          CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK))
                        */

    uint16 CSD_Touchpad_GetTrackpadValueZ(uint32 axisID, uint32 fingerID);
    uint16 CSD_Touchpad_Get16bitDiffCountData(uint32 sensor);
    uint16 CSD_Touchpad_Get16bitFingerThreshold(uint32 widget);
    uint16 CSD_Touchpad_Get16bitNoiseThreshold(uint32 widget);

    /*  Pre-compiler Functions Prototypes     */
    void AdvCentroidInit_P4_v2_40(void);
    uint32 GetDoubleCentroidPos_P4_v2_40(CSD_Touchpad_ADV_CENTROID_P4_v2_40 *config, CSD_Touchpad_POSITION_STRUCT *pos);
#endif /* (0u != CSD_Touchpad_TRACKPADS_WITH_GESTURES) */


/***************************************
*     Vars with External Linkage
***************************************/
#if(0u != (CSD_Touchpad_XY_ADAPTIVE_IIR_MASK & CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK))
    extern ADAPTIVE_FILTER_OPS_P4_v2_40 CSD_Touchpad_adpFltOptions;
#endif /* (0u != (CSD_Touchpad_XY_ADAPTIVE_IIR_MASK & CSD_Touchpad_TP_GESTURE_POS_FILTERS_MASK)) */

#if (0u != CSD_Touchpad_TRACKPADS_WITH_GESTURES)
    extern CSD_Touchpad_TMG_CONFIG_STRUCT CSD_Touchpad_gesturesConfig;
#endif /* (0u != CSD_Touchpad_TRACKPADS_WITH_GESTURES) */


#endif /* CY_CAPSENSE_CSD_GESTURE_CSD_Touchpad_H */


/* [] END OF FILE */
